<?php
/**
 * @category   Estrada Virtual
 * @package    Controller
 * @copyright  Copyright (c) 2010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @version    $Id$
*/

/**
 * Controller default from Estrada Virtual
 * @author Cristiano Teles <cristianoteles@estradavirtual.com.br>
 *
 */
class Ev_Controller_Action extends Zend_Controller_Action
{
    /**
     * @var Zend_Controller_Action_Helper_FlashMessenger FlashMessenger
     * @access private
     */
    private $_flashMessenger;

    public function init()
    {
        // Messages
        $this->view->messages  = $this->_getMessages();
        $this->_helper->FlashMessenger->clearMessages();

        // Verifica se foi feira uma requisição "ajax" e se for desabilita o layout
        if($this->getRequest()->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
            $this->view->assign( 'bAjax', true );
            $this->getResponse()->setHeader('App-Module', $this->getRequest()->getModuleName(), true);
            $this->getResponse()->setHeader('App-Controller', $this->getRequest()->getControllerName(), true);
            $this->getResponse()->setHeader('App-Action', $this->getRequest()->getActionName(), true);
        }
        /*
         * @TODO ver em que momentos usar o cache
        $this->getResponse()->setHeader(‘Expires’, ”, true);
        $this->getResponse()->setHeader(‘Cache-Control’, ‘public’, true);
        $this->getResponse()->setHeader(‘Cache-Control’, ‘max-age=3800′);
        $this->getResponse()->setHeader(‘Pragma’, ”, true);
         */
    }

    /**
     * Set message in flashMessenger and/or redirect
     * @access protected
     * @param string_type $msg Message
     * @param string $url URL
     * @return Zend_Controller_Action_Helper_FlashMessenger
     */
    protected function _addMessage($msg, $url = null)
    {
        $translate = Zend_Registry::get('Zend_Translate');
        $this->_helper->flashMessenger($translate->_($msg));
        if (null !== $url) {
            $this->_redirect($url);
        }
    }

    /**
     * Get messages in flashMessenger
     * @access protected
     * @return array
     */
    protected function _getMessages()
    {
        return $this->_helper->FlashMessenger->getMessages();
    }

    public function log($msg, $code)
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasPluginResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        $log->log($msg, $code);
        return $log;
    }
}
